package com.stox.charting.ui.swing.action;

import java.awt.event.ActionEvent;
import java.util.List;

import com.stox.charting.ui.swing.model.price.PriceChartModel;
import com.stox.charting.ui.swing.view.ChartingInternalFrameView;
import com.stox.core.model.IBar;
import com.stox.core.model.intf.HasBars;
import com.stox.ioc.Container;

public class ReplaceChartAction extends AbstractChartAction {

	private NewChartAction newChartAction;
	
	public ReplaceChartAction() {
		this.newChartAction = Container.get(NewChartAction.class);
	}
	
	@Override
	public void actionPerformed(ActionEvent e, List<HasBars> hasBars) {
		ChartingInternalFrameView view = ChartingInternalFrameView.getLastSelectedView();
		if(view == null){
			newChartAction.actionPerformed(e, hasBars);
		}else{
			PriceChartModel<? extends IBar> model = view.getPriceChartView().getPriceChart().getModel(); 
			model.setHasBars(hasBars.get(0));
			model.fireChartModelChangedEvent("chartSpan");
			model.fireChartModelChangedEvent("barSpan");
			model.fireChartModelChangedEvent("barType");
			
			view.repaintChartViews();
		}
	}
	
	
	@Override
	public boolean validateMaximumChartLimit(List<HasBars> hasBars,
			int maxChartsLimit) {
		return super.validateMaximumChartLimit(hasBars, 1);
	}
}
